var teamId;
var teamName;
var tmGameId;
var gameJudgeList={};
var ourScore;
var oppScore;
$(document).ready(function(){ 
	$('#header').animate({'top':'0px'},400);
	teamId = args.teamId; //取得id的值
	teamName = args.teamName;
	tmGameId = args.tmGameId;
	
	initPage();
	
	$("#submitBtn").click(function(){
		ourScore = parseInt($('#ourScore').val());
		oppScore = parseInt($('#oppScore').val());
		if(ourScore==undefined||isNaN(ourScore)==true||ourScore<0){
			alert("填入有效的本方比分。");return false;
		}
		if(oppScore==undefined||isNaN(oppScore)==true||oppScore<0){
			alert("填入有效的对方比分。");return false;
		}
		var jsonStr = JSON.stringify(gameJudgeList);
		$.post(path+"/soccer/teamGameAction!gameJudgeAssignSubmit.action",{ourScore:ourScore,oppScore:oppScore,teamId:teamId,tmGameId:tmGameId,jsonStr:jsonStr}, function(data){
			strMessage = data.strMessage;
			//$('#strMessage').text(strMessage);
			alert(strMessage);
		},'json');
	});
	$("#addArriveBtn").click(function(){
		var timestamp = (new Date()).valueOf();
		var backURL="/soccer/teamGameAssignJudge.jsp";
		window.location.href=path+"/soccer/teamGameUnarriveList.jsp?teamId="+teamId+"&tmGameId="+tmGameId+"&backURL="+backURL+"&timestamp="+timestamp;
	});
});
function deleteArrive(scoreUserId){
	$.post(path+"/soccer/teamGameAction!deleteArriveDataByGameUser.action",{teamId:teamId,tmGameId:tmGameId,arriveUserId:scoreUserId}, function(data){
		strMessage = data.strMessage;
		//$('#strMessage').text(strMessage);
		alert(strMessage);
		initPage();
	},'json');
}
function goBack(){
	window.location.href=path+"/soccer/teamGame.jsp?teamId="+teamId+"&teamName="+teamName;
}
function judgeFlagChanged(index,obj){
	if(obj.checked==true){
		gameJudgeList[index].judgeFlag="1";
	}else{
		gameJudgeList[index].judgeFlag="0";
	}	
}
function playFlagChanged(index,obj){
	if(obj.checked==true){
		gameJudgeList[index].playFlag="1";
	}else{
		gameJudgeList[index].playFlag="0";
	}	
}
function initPage(){
	//初始化比分
	$.getJSON(path+"/soccer/teamGameAction!selectTeamGameById.action",{tmGameId: tmGameId}, function(data){
		var tmTeamGame = data.tmTeamGame;
		ourScore = tmTeamGame.ourScore;
		oppScore = tmTeamGame.oppScore;
		$('#ourScore').val(ourScore);
		$('#oppScore').val(oppScore);
	});
	var paraData = {teamId:teamId,tmGameId:tmGameId};
	$.getJSON(path+"/soccer/teamGameAction!selectForJudgeAssign.action",paraData, function(data){
		gameJudgeList = data.gameJudgeList;
	  	var gameJudgeHtml=[];
	  	$.each(gameJudgeList,function(index,obj){
	  		gameJudgeHtml.push("<tr>");
	  		gameJudgeHtml.push("<td nowrap='nowrap' style='font-size:20px'>"+obj.scoreUserNo+"</td>");
	  		gameJudgeHtml.push("<td nowrap='nowrap' style='font-size:20px'>"+obj.scoreUserName+"</td>");
	  		gameJudgeHtml.push("<td style='text-align:center'>");
	  		if(obj.playFlag=='1'){
	  			gameJudgeHtml.push("<input id='gameJudgeList["+index+"].playFlag' type='checkbox' style='height:20px;width:20px' checked='checked' onclick='playFlagChanged("+index+",this)'/>");
	  		}else{
	  			gameJudgeHtml.push("<input id='gameJudgeList["+index+"].playFlag' type='checkbox' style='height:20px;width:20px' onclick='playFlagChanged("+index+",this)'/>");
	  		}
	  		gameJudgeHtml.push("</td>");
	  		gameJudgeHtml.push("<td style='text-align:center'>");
	  		if(obj.judgeFlag=='1'){
	  			gameJudgeHtml.push("<input id='gameJudgeList["+index+"].judgeFlag' type='checkbox' style='height:20px;width:20px' checked='checked' onclick='judgeFlagChanged("+index+",this)'/>");
	  		}else{
	  			gameJudgeHtml.push("<input id='gameJudgeList["+index+"].judgeFlag' type='checkbox' style='height:20px;width:20px' onclick='judgeFlagChanged("+index+",this)'/>");
	  		}  		
	  		gameJudgeHtml.push("</td>");
	  		gameJudgeHtml.push("<td nowrap='nowrap' style='font-size:20px'>");
	  		gameJudgeHtml.push("<input id='gameJudgeList["+index+"].arriveFlag' name='' type='button'  value='-' onclick='deleteArrive("+obj.scoreUserId+")'/>");
	  		gameJudgeHtml.push("</td>");
	  		gameJudgeHtml.push("</tr>");
	  	});
	  	$("#gameJudgeBody").html(gameJudgeHtml.join(""));  
	});
}
//限制只能输入数字
function validateNumber(e,index){
	var code = parseInt(window.event ? e.keyCode:e.which);
    if (code >= 96 && code <= 105 || code >= 48 && code <= 57 || code == 8 || code==110 || code==190) {  
        return true;  
    } else {  
    	alert("只能输入数字！");
    	var t = $("input[id='gameTechList["+index+"].techScore']");
    	t.val(gameTechList[index].techScore);
        return false;
    }
}